import React from 'react';
import { Table, message, Spin } from 'antd';
import Api from 'Utils/api';
import EqualBorder from './../../../CommonComponents/EqualBorder';


const PAGE_SIZE = 10;
class System extends React.PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      dataCount: 0,
      data: [],
      listPage: 1,
      isLoading: true,
    };
    this.condition = { offset: 0, limit: PAGE_SIZE };
    this.initList = ::this.initList;
  }
  componentDidMount() {
    this.initList();
  }
  initList() {
    const api = new Api({
      method: 'GET',
      path: '/permissions',
      query: { ...this.condition },
      success: (json) => {
        this.setState({ isLoading: false, listPage: ((this.condition.offset / PAGE_SIZE) + 1), data: json.data_list, dataCount: json.total_count });
      },
      error: (msg) => {
        this.setState({
          isLoading: false,
        });
        message.error(msg);
      }
    });
    api.run();
  }
  onListPageChange(page) {
    this.setState({ listPage: page, isLoading: true }, this.initList.bind(this));
    this.condition.offset = (page - 1) * PAGE_SIZE;
  }
  generatorColumns() {
    return [
      {
        title: '权限名',
        dataIndex: 'name',
        width: '20%',
        render: text => (
          <span>{text}</span>
        )
      },
      {
        title: '描述',
        dataIndex: 'description',
        render: text => (
          <span >{text}</span >
        )
      },
    ];
  }
  render() {
    const columns = this.generatorColumns();
    return (
      <EqualBorder
        content={
          <Spin size="large" spinning={this.state.isLoading} tip="加载中...">
            <Table
              rowKey={record => record.id}
              columns={columns}
              bordered
              dataSource={this.state.data}
              pagination={{
                pageSize: PAGE_SIZE,
                total: this.state.dataCount,
                current: this.state.listPage,
                onChange: this.onListPageChange.bind(this)
              }}
            />
          </Spin>
        }
      />
    );
  }
}

export default System;
